package cn.daniellee.plugin;

import lib.PatPeter.SQLibrary.Database;
import lib.PatPeter.SQLibrary.MySQL;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QuickShopFix extends JavaPlugin {

	private static String prefix;

	public void onEnable(){
		loadConfig();
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> QuickShopFix Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
		getLogger().info(" ");

		Bukkit.getPluginCommand("quickshopfix").setExecutor(this);
	}

	private void loadConfig() {
		getLogger().info("[QuickShopFix] Loading config...");
		prefix = "&7[&bQuickShopFix&7] &3: &r";
	}

	@Override
	public void onDisable() {
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> QuickShopFix Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(" ");
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		sender.sendMessage((prefix + "商店插件修复开始").replace("&", "§"));
		Connection connection = null;
		try {
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/minecraft?useUnicode=true&characterEncoding=utf8", "root", "Moziyu95");
			ResultSet shopsSet = connection.prepareStatement("select * from shops").executeQuery();
			ResultSetMetaData shopsSetMetaData = shopsSet.getMetaData();
			List<Map<String, Object>> shopsList = new ArrayList<>();
			while (shopsSet.next()) {
				Map<String, Object> rowData = new HashMap<>();
				for (int i = 1; i <= shopsSetMetaData.getColumnCount(); i++) {
					rowData.put(shopsSetMetaData.getColumnName(i), shopsSet.getObject(i));
				}
				shopsList.add(rowData);
			}
			PreparedStatement shopsStatement = connection.prepareStatement("insert into qs_shops (owner,price,itemConfig,x,y,z,world,unlimited,type) values (?,?,?,?,?,?,?,?,?)");
			for (Map item : shopsList) {
				shopsStatement.setString(1, "{\"owner\":\"" + Bukkit.getOfflinePlayer((String) item.get("owner")).getUniqueId().toString() + "\",\"staffs\":[]}");
				shopsStatement.setDouble(2, (Double) item.get("price"));
				String itemConfig = ((String) item.get("itemConfig")).replace("1631", "1968");
				String itemMaterial = itemConfig.substring(itemConfig.indexOf("type: ") + 6);
				itemMaterial = itemMaterial.substring(0, itemMaterial.indexOf("\n"));
				getLogger().info(itemMaterial);
				if (Material.getMaterial(itemMaterial) == null) continue;
				shopsStatement.setString(3, itemConfig);
				shopsStatement.setInt(4, (Integer) item.get("x"));
				shopsStatement.setInt(5, (Integer) item.get("y"));
				shopsStatement.setInt(6, (Integer) item.get("z"));
				shopsStatement.setString(7, (String) item.get("world"));
				shopsStatement.setBoolean(8, (Boolean) item.get("unlimited"));
				shopsStatement.setBoolean(9, (Boolean) item.get("type"));
				shopsStatement.execute();
			}
			ResultSet messagesSet = connection.prepareStatement("select * from messages").executeQuery();
			ResultSetMetaData messagesSetMetaData = messagesSet.getMetaData();
			List<Map<String, Object>> messagesList = new ArrayList<>();
			while (messagesSet.next()) {
				Map<String, Object> rowData = new HashMap<>();
				for (int i = 1; i <= messagesSetMetaData.getColumnCount(); i++) {
					rowData.put(messagesSetMetaData.getColumnName(i), messagesSet.getObject(i));
				}
				messagesList.add(rowData);
			}
			PreparedStatement messageStatement = connection.prepareStatement("insert into qs_messages (owner,message,time) values (?,?,?)");
			for (Map item : messagesList) {
				messageStatement.setString(1, Bukkit.getOfflinePlayer((String) item.get("owner")).getUniqueId().toString());
				messageStatement.setString(2, (String) item.get("message"));
				messageStatement.setLong(3, (Long) item.get("time"));
				messageStatement.execute();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException ignored) { }
			}
		}
		sender.sendMessage((prefix + "商店插件修复完成").replace("&", "§"));
		return true;
	}

}